//
//  TZzhuceMima.m
//  XMLYFM
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 zzl. All rights reserved.
//

#import "TZzhuceMima.h"
#import "CoreDataManager.h"
#import "User.h"
#import "MineController.h"
@interface TZzhuceMima ()
@property(nonatomic,retain)NSManagedObjectContext *managerObjectContext;
@property(nonatomic,retain)NSMutableArray *resultArr;//搜索结果
@end

@implementation TZzhuceMima

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.MimaLabel.layer.borderColor = [UIColor grayColor].CGColor;
    self.MimaLabel.layer.borderWidth = 1;
    _managerObjectContext = [[CoreDataManager shareManager]managedObjectContext];
//    NSLog(@"🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶%@",[[CoreDataManager shareManager]applicationDocumentsDirectory]);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


  
    
    

- (IBAction)mimaACtion:(UIButton *)sender {
    //查询请求
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    //获取要查询的实体的描述
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:_managerObjectContext];
    
    //对fetchRequest 设置查询的实体
    [fetchRequest setEntity:entity];
    //第一个参数:按那个字段排序
    //第二个参数 是否升序的意思
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    //从数据库中搜索满足上面条件的元素
    NSArray *fetchedObjects = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱%@",error);
    }
    
    _resultArr = [NSMutableArray arrayWithArray:fetchedObjects];
    for (User *user in _resultArr) {
        if ([user.name isEqualToString:self.mima]) {
            
            user.mima = self.mimaField.text;
            //保存
            user.phoneNumber = self.phoneNumber;
            [[CoreDataManager shareManager]saveContext];
           
            [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"logon"];
            [[NSUserDefaults standardUserDefaults]setValue:self.mima forKey:@"person"];
            
             break;
        }
       
    }
    MineController *mine = [[MineController alloc]init];
    mine.nameStr = self.mima;
    [self.navigationController pushViewController:mine animated:YES];
    
}
@end
